package com.vecspace.dayu.code.ui.pojo;

import java.util.Date;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.vecspace.data.model.IdEntity;
import com.vecspace.data.model.RowMapper;
import com.vecspace.dicttable.DictAll;

import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.Polymorphism;
import org.hibernate.annotations.PolymorphismType;
import org.hibernate.annotations.Type;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/** DAYU-表单列
 * @author wanghua
 */
@Entity
@Polymorphism(type = PolymorphismType.EXPLICIT)
@Table(name = "DAYU_CODE_UI_FORM_FIELD")
public class FormField implements IdEntity, java.io.Serializable {
	
	private static final long serialVersionUID = -1824610768442218109L;
	public FormField(){
	}
	
	private Long id;
	/**逻辑ID*/
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "ID")
	public Long getId(){
		return id;
	}
	/**逻辑ID*/
	public void setId(Long value){
		this.id = value;
	}

	private Long formId;
	/**表单ID*/
	@Column(name = "FORM_ID")
	public Long getFormId(){
		return formId;
	}
	/**表单ID*/
	public void setFormId(Long value){
		this.formId = value;
	}

	private Long columnId;
	/**字段ID*/
	@Column(name = "COLUMN_ID")
	public Long getColumnId(){
		return columnId;
	}
	/**字段ID*/
	public void setColumnId(Long value){
		this.columnId = value;
	}

	private String inputTip;
	/**输入提示*/
	@Column(name = "INPUT_TIP")
	public String getInputTip(){
		return inputTip;
	}
	/**输入提示*/
	public void setInputTip(String value){
		this.inputTip = value;
	}

	private String columnLabel;
	/**标签*/
	@Column(name = "COLUMN_LABEL")
	public String getColumnLabel(){
		return columnLabel;
	}
	/**标签*/
	public void setColumnLabel(String value){
		this.columnLabel = value;
	}

	private String columnGroup;
	/**表单分组*/
	@Column(name = "COLUMN_GROUP")
	public String getColumnGroup(){
		return columnGroup;
	}
	/**表单分组*/
	public void setColumnGroup(String value){
		this.columnGroup = value;
	}

	private Boolean newRow;
	/**强制新起一行*/
	@Column(name = "NEW_ROW")@Type(type="org.hibernate.type.NumericBooleanType")
	public Boolean getNewRow(){
		return newRow;
	}
	/**强制新起一行*/
	public void setNewRow(Boolean value){
		this.newRow = value;
	}

	private Integer colSpan;
	/**列数*/
	@Column(name = "COL_SPAN")
	public Integer getColSpan(){
		return colSpan;
	}
	/**列数*/
	public void setColSpan(Integer value){
		this.colSpan = value;
	}

	private Integer rowSpan;
	/**行数*/
	@Column(name = "ROW_SPAN")
	public Integer getRowSpan(){
		return rowSpan;
	}
	/**行数*/
	public void setRowSpan(Integer value){
		this.rowSpan = value;
	}

	private String formElementType;
	
	public static String FormElementTypeCode = "DAYU-CODE-011";
	
	/**编辑输入框类型*/
	@Column(name = "FORM_ELEMENT_TYPE")
	public String getFormElementType(){
		return formElementType;
	}
	/**编辑输入框类型*/
	public void setFormElementType(String value){
		this.formElementType = value;
	}

	@Transient
	public String getFormElementTypeCaption(){
		return DictAll.getCachedItemName(FormElementTypeCode, formElementType);
	}

	private Boolean required;
	/**是否必填*/
	@Column(name = "REQUIRED")@Type(type="org.hibernate.type.NumericBooleanType")
	public Boolean getRequired(){
		return required;
	}
	/**是否必填*/
	public void setRequired(Boolean value){
		this.required = value;
	}

	private String validationRules;
	
	public static String ValidationRulesCode = "DAYU-CODE-016";
	
	/**验证规则*/
	@Column(name = "VALIDATION_RULES")
	public String getValidationRules(){
		return validationRules;
	}
	/**验证规则*/
	public void setValidationRules(String value){
		this.validationRules = value;
	}
	
	/**验证规则*/
	@Transient
	public String getValidationRulesCaption(){
		String captions = "";
		if(validationRules != null && validationRules.length() != 0) {
			String[] rules = validationRules.trim().split(";");
			for(String rule : rules) {
				String caption = DictAll.getCachedItemName(ValidationRulesCode, rule);
				captions = captions + caption +"," ;
			}
		}
		if(captions.length() > 0) captions = captions.substring(0, captions.length() - 1);
		return captions;
	}

	private Integer ordnum;
	/**顺序号*/
	@Column(name = "ORDNUM")
	public Integer getOrdnum(){
		return ordnum;
	}
	/**顺序号*/
	public void setOrdnum(Integer value){
		this.ordnum = value;
	}

	private Date createTime;
	/**记录创建时间*/
	@JsonIgnore(true)
	@Column(name = "CREATE_TIME")
	public Date getCreateTime(){
		return createTime;
	}
	/**记录创建时间*/
	public void setCreateTime(Date value){
		this.createTime = value;
	}
	@Transient
	@JsonIgnore(true)
	public String getCreateTimeStr(){
		if(createTime == null){
			return "";
		}
		String exp = "yyyy-MM-dd";
		String timestr = new java.text.SimpleDateFormat(exp).format(createTime);
		return timestr;
	}

	private String createUser;
	/**记录创建人ID*/
	@Column(name = "CREATE_USER")
	public String getCreateUser(){
		return createUser;
	}
	/**记录创建人ID*/
	public void setCreateUser(String value){
		this.createUser = value;
	}

	
	public void parseFrom(Map<String, Object> row){
		RowMapper rowMapper = new RowMapper(row);
		this.id = rowMapper.getLong("ID");
		this.formId = rowMapper.getLong("FORM_ID");
		this.columnId = rowMapper.getLong("COLUMN_ID");
		this.inputTip = rowMapper.getString("INPUT_TIP");
		this.columnLabel = rowMapper.getString("COLUMN_LABEL");
		this.columnGroup = rowMapper.getString("COLUMN_GROUP");
		this.newRow = rowMapper.getBoolean("NEW_ROW");
		this.colSpan = rowMapper.getInteger("COL_SPAN");
		this.rowSpan = rowMapper.getInteger("ROW_SPAN");
		this.formElementType = rowMapper.getString("FORM_ELEMENT_TYPE");
		this.required = rowMapper.getBoolean("REQUIRED");
		this.validationRules = rowMapper.getString("VALIDATION_RULES");
		this.ordnum = rowMapper.getInteger("ORDNUM");
		this.createTime = rowMapper.getDate("CREATE_TIME");
		this.createUser = rowMapper.getString("CREATE_USER");
	}
	
//	private String columnName;
//	@Transient
//	public String getColumnName() {
//		return columnName;
//	}
//	public void setColumnName(String columnName) {
//		this.columnName = columnName;
//	}
//
//	private DataColumn column;
//
//	@Transient
//	@JsonIgnore
//	public DataColumn getColumn() {
//		return column;
//	}
//	public void setColumn(DataColumn column) {
//		this.column = column;
//	}
	
//	private boolean edited;
//	@Transient
//	public boolean isEdited() {
//		return edited;
//	}
//	public void setEdited(boolean edited) {
//		this.edited = edited;
//	}
//
//	private boolean deleted;
//	@Transient
//	public boolean isDeleted() {
//		return deleted;
//	}
//	public void setDeleted(boolean deleted) {
//		this.deleted = deleted;
//	}
	@Override
	public boolean equals(Object obj) {
		if(obj == null)return false;
		if(id == null)return false;
		FormField other = (FormField)obj;
		return id.equals(other.id);
	}

	@Override
	public int hashCode() {
		if(id == null)return 0;
		return id.hashCode();
	}
}
